package main

import (
	"context"
	"fmt"
	"log"
	"time"

	pb "github.com/plutov/packagemain/grpc-invoke-java/gen" // 替换为实际路径
	"google.golang.org/grpc"
)

func main() {
	// 连接到 Java gRPC 服务器
	conn, err := grpc.Dial("192.168.65.130:9090", grpc.WithInsecure())
	if err != nil {
		log.Fatalf("Did not connect: %v", err)
	}
	defer conn.Close()

	client := pb.NewPaginationServiceClient(conn)

	// 创建分页请求
	req := &pb.PageRequest{
		PageNumber: 1,
		PageSize:   10,
	}

	// 设置上下文超时
	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()

	// 调用 GetItems 方法
	res, err := client.GetItems(ctx, req)
	if err != nil {
		log.Fatalf("Could not get items: %v", err)
	}

	// 显示返回的分页数据
	fmt.Printf("Current Page: %d\n", res.GetCurrentPage())
	fmt.Printf("Total Items: %d\n", res.GetTotalItems())
	fmt.Printf("Total Pages: %d\n", res.GetTotalPages())
	fmt.Printf("Page Size: %d\n", res.GetPageSize())
	for _, item := range res.GetItems() {
		fmt.Printf("Item ID: %d, Name: %s\n", item.GetId(), item.GetName())
	}
}
